package gui;

import business.UserManager;
import entity.SearchResult;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

@Named(value = "searchResultBean")
@RequestScoped
public class SearchResultBean {

    @EJB
    private UserManager userManager;

    private String keyword;
    List<SearchResult> searchResults = new ArrayList<>();

    public SearchResultBean() {
    }

    public void doSearch() {
        if (keyword != null && !keyword.isEmpty() && Pattern.matches("[a-zA-Z0-9]*", keyword)) {
            searchResults = userManager.search(keyword);
            SearchResult resultToExclude = new SearchResult(userManager.getLoggedUser().getUsername(), null, null);
            searchResults.remove(resultToExclude);
        }
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public List<SearchResult> getSearchResults() {
        return searchResults;
    }
}
